﻿using System.Globalization;
using System.Threading;
using System.Web.Mvc;
using System.Web.Routing;
using VideoSeller.Business.Data;
using VideoSeller.Business.Models;
using VideoSeller.Core.Configuration;

namespace VideoSeller.Web.Controllers
{
	public abstract class BaseController : Controller
	{
		public MembershipUser CurrentMembershipUser
		{
			get
			{
				if (User != null && User.Identity.IsAuthenticated)
				{
					var membershipUserModel = IoC.Resolve<MembershipUserModel>();
					return membershipUserModel.GetMembershipUser(User.Identity.Name);
				}

				return null;
			}
		}

		public Client CurrentClient
		{
			get
			{
				if (User != null && User.Identity.IsAuthenticated)
				{
					var clientModel = IoC.Resolve<ClientModel>();
					return clientModel.GetClient(CurrentMembershipUser.UserId);
				}

				return null;
			}
		}

		protected override void Initialize(RequestContext requestContext)
		{
			base.Initialize(requestContext);

			var userProfile = requestContext.HttpContext.Profile;
			var userCulture = userProfile != null ? userProfile["Culture"].ToString() : string.Empty;

			if (!string.IsNullOrEmpty(userCulture) && userCulture != CultureInfo.CurrentUICulture.ToString())
			{
				Thread.CurrentThread.CurrentCulture = new CultureInfo(userCulture);
				Thread.CurrentThread.CurrentUICulture = new CultureInfo(userCulture);
			}
		}
	}
}